//
//  oldFamilySendOrderViewController.m
//  laonianbangfamily
//
//  Created by 勇拓 李 on 18/3/23.
//  Copyright © 2018年 zaiyou. All rights reserved.
//

#import "oldFamilySendOrderViewController.h"
#import "oldAddressChooseViewController.h"
#import "oldFamilyParentListTableViewController.h"
#import "oldVideoViewController.h"
#import "QNuploader.h"
#import "oldFileManager.h"

#define NUM 150

@interface oldFamilySendOrderViewController ()<UITextViewDelegate, oldVideoViewControllerDelegate>
@property (weak, nonatomic) IBOutlet UILabel *label_service;
@property (weak, nonatomic) IBOutlet UILabel *label_mobile;
@property (weak, nonatomic) IBOutlet UILabel *label_parent;
@property (weak, nonatomic) IBOutlet UILabel *label_address;
@property (weak, nonatomic) IBOutlet UIView *view_parent;
@property (weak, nonatomic) IBOutlet UIView *view_address;
@property (weak, nonatomic) IBOutlet UITextView *text_view;
@property (weak, nonatomic) IBOutlet UILabel *label_placeholder;
@property (weak, nonatomic) IBOutlet UILabel *label_number;
@property (weak, nonatomic) IBOutlet UIButton *btn_commit;
@property (weak, nonatomic) IBOutlet UIView *view_video;

@property (nonatomic, copy)NSString *url;

@end

@implementation oldFamilySendOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    [self initData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - initView
- (void)initView
{
    self.navigationItem.title = @"确认下单";
    
    _text_view.layer.borderWidth = 1.f;
    _text_view.layer.borderColor = Color_With_Rgb(230, 230, 230, 0.6).CGColor;
    _btn_commit.layer.cornerRadius = 6.f;
    _text_view.delegate = self;
    
    UITapGestureRecognizer * gestureAddress = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gesAddress:)];
    [_view_address addGestureRecognizer:gestureAddress];
    _view_address.userInteractionEnabled = YES;
    
    UITapGestureRecognizer * gestureParent = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gesParent:)];
    [_view_parent addGestureRecognizer:gestureParent];
    _view_parent.userInteractionEnabled = YES;
    
    UITapGestureRecognizer * gestureVideo = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gesVideo:)];
    [_view_video addGestureRecognizer:gestureVideo];
    _view_video.userInteractionEnabled = YES;
    
    [_btn_commit addTarget:self action:@selector(btn_commit_tapped) forControlEvents:UIControlEventTouchDown];
    
    _label_mobile.hidden = YES;
}

#pragma mark - initData
- (void)initData
{
    _label_service.text = _serviceInfo.cname;
}

#pragma mark - address
- (void)gesAddress:(id)sender
{
    oldAddressChooseViewController *vc = [oldAddressChooseViewController new];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)setAddressInfo:(oldAddressInfo *)addressInfo
{
    _addressInfo = addressInfo;
    _label_address.text = addressInfo.address;
}

#pragma mark - parent
- (void)gesParent:(id)sender
{
    oldFamilyParentListTableViewController *vc = [oldFamilyParentListTableViewController new];
    vc.ifComeFromOrder = YES;
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)setServer:(oldServer *)server
{
    _server = server;
    _label_parent.text = [NSString stringWithFormat:@"%@(%@)", server.uname, server.userRelation.relation];
    _label_mobile.hidden = NO;
    _label_mobile.text = server.mobile;
}

#pragma mark - video
- (void)gesVideo:(id)sender
{
    oldVideoViewController *vc = [oldVideoViewController new];
    vc.delegate = self;
    vc.url = _url;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)videoComplete:(NSString *)url
{
    _url = url;
}

#pragma mark - btn_commit_tapped
- (void)btn_commit_tapped
{
    if (!_addressInfo.aid) {
        [oldHub warningUser:@"请选择地址！"];
        return;
    }
    
    if (!_server.uid) {
        [oldHub warningUser:@"请选择服务对象！"];
        return;
    }
    
    if (!_serviceInfo.cid) {
        [oldHub warningUser:@"请选择服务类型！"];
        return;
    }
    
    [oldHub waiting:@"发送中.."];
    
    if(_url)
    {
        WeakSelf
        
        [QiNiuShareInstance uploadImageToQNFilePath:_url type:Video_file block:^(NSString *url) {
            if (url) {
                [oldFileManager removeFileByPath:weakSelf.url];//清除缓存
                [weakSelf SendDemand:url];
            }
            else
            {
                [oldHub warningUser:@"上传视频失败请重试"];
            }
        }];
    }
    else
    {
        [self SendDemand:nil];
    }
}

- (void)SendDemand:(NSString *)url
{
    WeakSelf
    
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys: _server.uid, @"attendeeUid", _serviceInfo.cid, @"cid", _addressInfo.aid, @"aid", nil];
    
    if (url) {
        [params setObject:url forKey:@"familyVideoUrl"];
    }
    if([_text_view.text length])
    {
        NSCharacterSet  *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
        NSString *desc = [_text_view.text stringByTrimmingCharactersInSet:set];
        [params setObject:desc forKey:@"remark"];
    }
    
    [OLDNetWork httpRequestWithPostPut:URL_Family_SendRequest params:params success:^(id data) {
        [oldHub hideHUD];
        [weakSelf.navigationController popToRootViewControllerAnimated:YES];
    } error:^(NSString *error) {
        [oldHub warningUser:error];
    }];
}

#pragma mark - textViewDelegate
- (void)textViewDidChange:(UITextView *)textView
{
    if ([textView.text length] > NUM) {
        textView.text = [textView.text substringToIndex:NUM];
    }
    _label_number.text = [NSString stringWithFormat:@"%lu/%d", [textView.text length], NUM];
    
    if (_label_placeholder) {
        if ([textView.text length] == 0) {
            [_label_placeholder setHidden:NO];
        }
        else
        {
            [_label_placeholder setHidden:YES];
        }
    }
}

@end
